$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/order/shelvesOrderlist',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '订单编号', name: 'ordernum', index: 'ordernum', width: 100 },
			{ label: '货架名称', name: 'shelvesname', index: 'shelvesname', width: 100 },
			{ label: '购买人', name: 'username', index: 'username', width: 100 },
			{ label: '支付时间', name: 'paytime', index: 'paytime', width: 100 },
			{ label: '订单金额', name: 'ordermoney', index: 'ordermoney', width: 60,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.ordermoney == null ||rowdata.ordermoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.ordermoney;
                    }
                } },
			{ label: '优惠金额', name: 'discountmoney', index: 'discountmoney', width: 60,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.discountmoney == null ||rowdata.discountmoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.discountmoney;
                    }
                } },
			{ label: '支付金额', name: 'paymoney', index: 'paymoney', width: 60,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.paymoney == null ||rowdata.paymoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.paymoney;
                    }
                } },
			{ label: '商品成本', name: 'cost', index: 'cost', width: 60,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.cost == null ||rowdata.cost == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.cost;
                    }
                } },
			{ label: '商品利润', name: 'profit', index: 'profit', width: 60,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.profit == null ||rowdata.profit == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.profit;
                    }
                } },
			{ label: '商品数量', name: 'count', index: 'count', width: 60 },
            { label: '操作', name: 'sy', width: 110,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.update("+ rowdata.id +")'>" +
                        "查看</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.Statistics(null,null,null,null,null);
    vm.queryGoodshelves();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            number:null,
            sheelvesid:null,
            buyer:null,
            startime:null,
            endtime:null
		},
		showList: true,
		title: null,
		order: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.order = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        Statistics: function (ordernum,shelvesid,buyer,startime,endtime) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/order/infoStatistics",
                data:{"ordernum":ordernum,"shelvesid":shelvesid,"buyer":buyer,"startime":startime,"endtime":endtime},
                success: function(r){
                    var ordermoney = 0;
                    var cost = 0;
                    var paymoney = 0;
                    var profit = 0;
                    if(r.shelvesorder !=null){
                        ordermoney = r.shelvesorder.ordermoney;
                        paymoney = r.shelvesorder.paymoney;
                        cost = r.shelvesorder.cost == null?0:r.shelvesorder.cost;
                        profit = r.shelvesorder.profit == null?0:r.shelvesorder.profit;
                    }
                    $("#ddje").text("￥"+ordermoney);
                    $("#zfje").text("￥"+paymoney);
                    $("#spcb").text("￥"+cost);
                    $("#splr").text("￥"+profit);
                }
            });
        },
		saveOrUpdate: function (event) {
			var url = vm.order.id == null ? "sys/order/save" : "sys/order/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.order),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/order/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        queryGoodshelves: function(){
            $("#sheelves_id").html("");
            $.get(baseURL + "sys/goodshelves/alllist", function(r){
                var temp = "<option value=''></option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].shelvesname+"</option>";

                });
                $("#sheelves_id").append(temp);

            });
        },
		getInfo: function(id){
			$.get(baseURL + "sys/order/info/"+id, function(r){
                vm.order = r.order;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    shelvesid:vm.q.sheelvesid,
                    ordernum:vm.q.number,
                    startime:vm.q.startime,
                    endtime:vm.q.endtime,
                    buyer:vm.q.buyer
                },
                gridComplete:function(){
                    vm.Statistics(vm.q.number,vm.q.sheelvesid,vm.q.buyer,vm.q.startime,vm.q.endtime);
                }
            }).trigger("reloadGrid");
		}
	}
});